]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
calendar: Use relative times for notification timestamps
authorFlorian Müllner <fmuellner@gnome.org>
Mon, 27 Feb 2017 01:17:29 +0000 (02:17 +0100)
committerFlorian Müllner <fmuellner@gnome.org>
Wed, 1 Mar 2017 09:55:21 +0000 (10:55 +0100)
For notifications in the message list, it is usually less relevant
when exactly it occurred, but how long ago. So rather than showing
the exact time and expecting the user to figuring out the timespan
themselves, change the format to something human readable.

https://bugzilla.gnome.org/show_bug.cgi?id=775763

js/misc/util.js
js/ui/calendar.js

index 0221e15af583bc99b9fd946b215abca4f8b64168..20d6f27f59e2ddf088a55253d0f989cbb5a6d4ac 100644 (file)
@@ -1,6 +1,7 @@
 // -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
 
 const Clutter = imports.gi.Clutter;
+const Gettext = imports.gettext;
 const Gio = imports.gi.Gio;
 const GLib = imports.gi.GLib;
 const Lang = imports.lang;
@@ -163,6 +164,41 @@ function _handleSpawnError(command, err) {
     Main.notifyError(title, err.message);
 }
 
+function formatTimeSpan(date) {
+    let now = GLib.DateTime.new_now_local();
+
+    let timespan = now.difference(date);
+
+    let minutesAgo = timespan / GLib.TIME_SPAN_MINUTE;
+    let hoursAgo = timespan / GLib.TIME_SPAN_HOUR;
+    let daysAgo = timespan / GLib.TIME_SPAN_DAY;
+    let weeksAgo = daysAgo / 7;
+    let monthsAgo = daysAgo / 30;
+    let yearsAgo = weeksAgo / 52;
+
+    if (minutesAgo < 5)
+        return _("Just now");
+    if (hoursAgo < 1)
+        return Gettext.ngettext("%d minute ago",
+                                "%d minutes ago", minutesAgo).format(minutesAgo);
+    if (daysAgo < 1)
+        return Gettext.ngettext("%d hour ago",
+                                "%d hours ago", hoursAgo).format(hoursAgo);
+    if (daysAgo < 2)
+        return _("Yesterday");
+    if (daysAgo < 15)
+        return Gettext.ngettext("%d day ago",
+                                "%d days ago", daysAgo).format(daysAgo);
+    if (weeksAgo < 8)
+        return Gettext.ngettext("%d week ago",
+                                "%d weeks ago", weeksAgo).format(weeksAgo);
+    if (yearsAgo < 1)
+        return Gettext.ngettext("%d month ago",
+                                "%d months ago", monthsAgo).format(monthsAgo);
+    return Gettext.ngettext("%d year ago",
+                            "%d years ago", yearsAgo).format(yearsAgo);
+}
+
 function formatTime(time, params) {
     let date;
     // HACK: The built-in Date type sucks at timezones, which we need for the
index b34f0ef9c6e627dad2181fdaf5a7d2c219450d10..721fc9f6775016b7cd5a4735f96abd9c336a4430 100644 (file)
@@ -957,9 +957,12 @@ const NotificationSection = new Lang.Class({
     },
 
     _createTimeLabel: function(datetime) {
-        let label = Util.createTimeLabel(datetime);
-        label.style_class = 'event-time',
-        label.x_align = Clutter.ActorAlign.START;
+        let label = new St.Label({ style_class: 'event-time',
+                                   x_align: Clutter.ActorAlign.START });
+        label.connect('notify::mapped', () => {
+            if (label.mapped)
+                label.text = Util.formatTimeSpan(datetime);
+        });
         return label;
     },